package classes.Windows;

import classes.Staff.Worker;
import db.Jdbc;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Auther: 程嘉明
 * @Date: 2018/8/26/026 16:07
 * @Description:注册窗口 注册可以为工人、经理根据人员类型添加
 */
public class Register extends Window {
    private JComboBox<String> jbox4;//籍贯选择省
    private JComboBox<String> jbox6;//籍贯选择市

    public Register(int length, int width, String title) throws Exception {
        super(length, width, title);
        //标题
        JLabel lab1 = new JLabel("员工注册");
        lab1.setFont(new Font("宋体", 1, 18));
        lab1.setBounds(0, 30, 800, 60);
        lab1.setOpaque(true);
        lab1.setBackground(Color.blue);
        this.addP(lab1);
        //ID,不可编辑，通过数据库查询获得
        JLabel lab2 = new JLabel(" ID：");
        Jdbc.getConnection();
        ResultSet rs = Jdbc.stat.executeQuery("select count(*) from t_gdryjbxx");//查询数据库中的记录数并且加一
        rs.next();
        int num = rs.getInt("count(*)");
        JTextField jt1 = new JTextField(ID_autoComplete(num));
        jt1.setEditable(false);//不可编辑
        lab2.setBounds(20, 110, 80, 60);
        jt1.setBounds(80, 122, 120, 40);
        lab2.setFont(new Font("宋体", 1, 18));
        lab1.setOpaque(true);
        this.addP(lab2);
        this.addP(jt1);
        //工作区域/工地标识
        JPanel p6 = new JPanel();
        p6.setLayout(null);
        p6.setBounds(320, 110, 400, 60);
        TitledBorder tb = BorderFactory.createTitledBorder("工地标识");//边框
        tb.setTitleJustification(TitledBorder.LEFT);
        p6.setBorder(tb);
        ButtonGroup bg = new ButtonGroup();
        JRadioButton randioButton1 = new JRadioButton("A区", true);
        randioButton1.setActionCommand("A");
        JRadioButton randioButton2 = new JRadioButton("B区");
        randioButton2.setActionCommand("B");
        JRadioButton randioButton3 = new JRadioButton("C区");
        randioButton3.setActionCommand("C");
        JRadioButton randioButton4 = new JRadioButton("D区");
        randioButton4.setActionCommand("D");
        JRadioButton randioButton5 = new JRadioButton("E区");
        randioButton5.setActionCommand("E");
        randioButton1.setBounds(10, 10, 80, 60);
        randioButton2.setBounds(80, 10, 80, 60);
        randioButton3.setBounds(160, 10, 80, 60);
        randioButton4.setBounds(240, 10, 80, 60);
        randioButton5.setBounds(320, 10, 80, 60);
        p6.add(randioButton1);
        p6.add(randioButton2);
        p6.add(randioButton3);
        p6.add(randioButton4);
        p6.add(randioButton5);
        bg.add(randioButton1);
        bg.add(randioButton2);
        bg.add(randioButton3);
        bg.add(randioButton4);
        bg.add(randioButton5);
        p6.setVisible(true);
        this.addP(p6);
        //证件类型
        JComboBox<String> jbox = new JComboBox<>(new String[]{"中华人民共和国居民身份证", "港澳居民来往内地通行证",
                "台湾居民来往大陆通行证", "军官证", "士兵证", "武警警官证", "护照", "外国人居留证", "外国人出入境证"});
        jbox.setBounds(80, 180, 180, 40);
        JLabel lab3 = new JLabel("证件类型:");
        lab3.setFont(new Font("宋体", 1, 12));
        lab3.setBounds(5, 180, 60, 40);
        this.addP(lab3);
        this.addP(jbox);
        //证件号码
        JLabel lab4 = new JLabel("证件号码:");
        lab4.setFont(new Font("宋体", 1, 14));
        lab4.setBounds(320, 180, 80, 40);
        JTextField jf2 = new JTextField();
        jf2.setBounds(400, 180, 220, 40);
        this.addP(lab4);
        this.addP(jf2);
        //人员类型
        JComboBox<String> jbox1 = new JComboBox<>(new String[]{"工人", "经理"});
        jbox1.setBounds(80, 250, 180, 40);
        JLabel lab5 = new JLabel("人员类别:");
        lab5.setFont(new Font("宋体", 1, 12));
        lab5.setBounds(5, 250, 60, 40);
        this.addP(lab5);
        this.addP(jbox1);
        //姓名
        JLabel lab6 = new JLabel("姓名:");
        lab6.setBounds(320, 250, 60, 40);
        lab6.setFont(new Font("宋体", 1, 12));
        JTextField jf3 = new JTextField();
        jf3.setBounds(400, 250, 120, 40);
        this.addP(jf3);
        this.addP(lab6);
        //性别
        JComboBox<String> jbox2 = new JComboBox<>(new String[]{"男", "女"});
        jbox2.setBounds(80, 320, 180, 40);
        JLabel lab7 = new JLabel("   性别:");
        lab7.setFont(new Font("宋体", 1, 12));
        lab7.setBounds(5, 320, 60, 40);
        this.addP(lab7);
        this.addP(jbox2);
        //国籍
        JComboBox<String> jbox3 = new JComboBox<>(new String[]{"中国", "美国", "日本", "韩国", "俄罗斯", "乌克兰", "德国", "西班牙", "阿根廷"
                , "波兰", "捷克", "斯洛伐克", "匈牙利", "奥地利", "瑞士", "列支敦士登", "菲律宾", "越南", "马来西亚", "越南", "新加坡", "泰国"});
        JLabel lab8 = new JLabel("   国籍:");
        lab8.setFont(new Font("宋体", 1, 12));
        lab8.setBounds(5, 390, 60, 40);
        jbox3.setBounds(80, 390, 180, 40);
        this.addP(jbox3);
        this.addP(lab8);
        //籍贯
        JLabel lab9 = new JLabel("   籍贯:");
        lab9.setFont(new Font("宋体", 1, 12));
        lab9.setBounds(300, 390, 60, 40);
        jbox4 = new JComboBox<>(new String[]{"安徽省", "北京市", "天津市", "重庆市", "上海市", "黑龙江省", "广东省", "海南省", "四川省", "云南省", "贵州省",
                "辽宁省", "吉林省", "湖南省", "湖北省", "山东省", "陕西省", "浙江省", "江苏省",
                "福建省", "青海省", "甘肃省", "山西省", "河北省", "河南省", "江西省", "台湾省", "内蒙古自治区", "新疆维吾尔自治区", "西藏自治区", "广西壮族自治区", "宁夏回族自治区", "香港", "澳门"});
        jbox4.setBounds(400, 390, 130, 40);
        jbox6 = new JComboBox<String>(new String[]{"黄山市", "合肥市", "芜湖市", "蚌埠市", "淮南市", "马鞍山市", "淮北市", "铜陵市", "安庆市", "滁州市", "阜阳市", "宿州市",});
        jbox6.setBounds(550, 390, 130, 40);
        this.addP(jbox6);
        this.addP(jbox4);
        this.jbox4Action();
        this.addP(lab9);
        //学历
        JComboBox<String> jbox5 = new JComboBox<>(new String[]{"博士", "硕士", "本科", "大专", "中专", "初中"});
        JLabel lab10 = new JLabel("   学历:");
        lab10.setFont(new Font("宋体", 1, 12));
        lab10.setBounds(300, 320, 60, 40);
        jbox5.setBounds(400, 320, 180, 40);
        this.addP(lab10);
        this.addP(jbox5);
        //返回、注册
        JButton jb1 = new JButton("返回");
        jb1.setBounds(20, 520, 120, 40);
        jb1.addActionListener(e -> {
            this.dispose();
        });
        this.addP(jb1);
        JButton jb2 = new JButton("完成");
        jb2.addActionListener(e -> {
            String ID = jt1.getText();//得到ID
            String GDBS = bg.getSelection().getActionCommand();//得到了GDBS
            String ZJLX = (String) jbox.getSelectedItem();//获得ZJLX
            String zjhm = jf2.getText();//证件号码
            long ZJHM = 0;
            if (jf2.getText().equals("")) {//证件号码没有填写
                JOptionPane.showMessageDialog(null, "证件号码没有填写！", "警告", JOptionPane.ERROR_MESSAGE);
            } else {
                ZJHM = Long.parseLong(zjhm);//341021199812235939
            }
            boolean zjlx=false;//判断证件类型与证件号码是否匹配
            if (ZJLX.equals("中华人民共和国居民身份证")) {//证件类型为身份证时长度为18或15
                Pattern pattern = Pattern.compile("(^\\d{18}$)|(^\\d{15}$)");//身份证的正则表达式支持18位或者15位
                Matcher m = pattern.matcher(zjhm);
                if (m.matches()) {
                    zjlx=true;
                }
            } else {
                Pattern pattern = Pattern.compile("(^\\d{8}$)");//非身份证的正则表达式支持8位
                Matcher m = pattern.matcher(zjhm);
                if (!m.matches()) {
                    zjlx=true;
                }
            }
            String rylb = (String) jbox1.getSelectedItem();//人员类别
            int RYLB = 0;
            if (rylb.equals("工人")) {
                RYLB = 1;
            } else {
                RYLB = 2;
            }
            String NAME = jf3.getText();//获得NAME
            String sex = (String) jbox2.getSelectedItem();//获得SEX
            int SEX = 0;
            if (sex.equals("男")) {
                SEX = 1;
            } else {
                SEX = 2;
            }
            String NATION = (String) jbox3.getSelectedItem();//获取NATION
            if (ZJLX.equals("护照") || ZJLX.equals("外国人居留证") || ZJLX.equals("外国人出入境证")) {//如果不是身份证或者，在国籍上特殊处理
                if (NATION.equals("中国")) {
                    JOptionPane.showMessageDialog(null, "国籍与证件类型不一致！", "警告", JOptionPane.ERROR_MESSAGE);
                }
            }
            if (!ZJLX.equals("护照") || !ZJLX.equals("外国人居留证") || !ZJLX.equals("外国人出入境证")) {//是中国类型的证件，则国籍必须为中国
                if (!NATION.equals("中国")) {
                    JOptionPane.showMessageDialog(null, "国籍与证件类型不一致！", "警告", JOptionPane.ERROR_MESSAGE);
                }
            }//34102119981223596458
            String NATLIVEPLACE = (String) jbox4.getSelectedItem() + (String) jbox6.getSelectedItem();//获取NATLIVEPLACE
            String WHCD = (String) jbox5.getSelectedItem();//获取WHCD
            if (!jf2.getText().equals("") || !jf3.getText().equals("")) {//信息完整,是否存在重復注册
                try {
                    boolean flag = Jdbc.stat.execute("select ZJHM from t_gdryjbxx");
                    if (flag) {//存在数据
                        ResultSet rs1 = Jdbc.stat.executeQuery("select ZJHM from t_gdryjbxx");//查询数据库中的所有ZJHM如果重复那么将不能注册
                        boolean exits=false;//判斷是否已經註冊
                        while (rs1.next()) {
                            if (ZJHM == rs1.getLong("ZJHM")) {
                                exits = true;//存在
                            }
                        }
                        if(exits==false){//身份证未注册
                            String pass1 = JOptionPane.showInputDialog(null, "请输入密码：\n", "完成密码填写", JOptionPane.PLAIN_MESSAGE);
                            String pass2 = JOptionPane.showInputDialog(null, "请再次输入密码：\n", "确认密码", JOptionPane.PLAIN_MESSAGE);
                            String PASS = pass1;
                            if (pass1 != null && pass2 != null) {//确认两次都相同
                                if (pass1.equals(pass2)) {//密码相同
                                    if(zjlx){//证件类型与证件号码符和
                                        try {
                                            new Worker(ID, GDBS, ZJLX, ZJHM, RYLB, NAME, SEX, NATION, NATLIVEPLACE, WHCD, PASS);//创建一个工人对象,在创建对象的同时会插入数据库
                                            JOptionPane.showMessageDialog(null, "注册成功！");
                                            this.dispose();
                                        } catch (Exception e1) {
                                            e1.printStackTrace();
                                        }
                                    }else{//类型与号码不同
                                        JOptionPane.showMessageDialog(null, "证件类型与证件号码不符！", "警告", JOptionPane.ERROR_MESSAGE);
                                    }
                                } else {//密码不同
                                    JOptionPane.showMessageDialog(null, "两次密码不相同！", "警告", JOptionPane.ERROR_MESSAGE);
                                }
                            }
                        }else{//证件注册
                            JOptionPane.showMessageDialog(null, "该证件已经注册！", "警告", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                }

            } else {
                JOptionPane.showMessageDialog(null, "信息不完整！", "警告", JOptionPane.ERROR_MESSAGE);
            }

        });
        jb2.setBounds(260, 450, 250, 60);
        this.addP(jb2);
    }

    public static void main(String[] args) throws Exception {
        new Register(800, 600, "注册窗口");
    }

    /**
     * 籍贯选择事件
     */
    public void jbox4Action() {
        jbox4.addActionListener(e -> {
            JComboBox combo = (JComboBox) e.getSource();
            if (combo.getSelectedItem().equals("北京市")) {
                jbox6.removeAllItems();
                jbox6.addItem("东城区");
                jbox6.addItem("西城区");
                jbox6.addItem("朝阳区");
                jbox6.addItem("丰台区");
                jbox6.addItem("海淀区");
            } else {
                if (combo.getSelectedItem().equals("山东省")) {
                    jbox6.removeAllItems();
                    jbox6.addItem("济南市");
                    jbox6.addItem("青岛市");
                    jbox6.addItem("潍坊市");
                    jbox6.addItem("淄博市");
                    jbox6.addItem("枣庄市");
                    jbox6.addItem("烟台市");
                    jbox6.addItem("泰安市");
                    jbox6.addItem("济宁市");
                    jbox6.addItem("威海市");
                    jbox6.addItem("聊城市");
                    jbox6.addItem("菏泽市");
                    jbox6.addItem("德州市");
                } else {
                    if (combo.getSelectedItem().equals("安徽省")) {
                        jbox6.removeAllItems();
                        jbox6.addItem("黄山市");
                        jbox6.addItem("合肥市");
                        jbox6.addItem("芜湖市");
                        jbox6.addItem("淮南市");
                        jbox6.addItem("马鞍山市");
                        jbox6.addItem("淮北市");
                        jbox6.addItem("铜陵市");
                        jbox6.addItem("宣城市");
                        jbox6.addItem("滁州市");
                        jbox6.addItem("阜阳市");
                        jbox6.addItem("宿州市");
                    } else {
                        jbox6.removeAllItems();
                        jbox6.addItem(combo.getSelectedItem() + "A市");
                        jbox6.addItem(combo.getSelectedItem() + "B市");
                        jbox6.addItem(combo.getSelectedItem() + "C市");
                        jbox6.addItem(combo.getSelectedItem() + "D市");
                        jbox6.addItem(combo.getSelectedItem() + "E市");
                        jbox6.addItem(combo.getSelectedItem() + "F市");
                        jbox6.addItem(combo.getSelectedItem() + "G市");
                    }
                }
            }
        });
    }

    /**
     * ID自动补齐
     */
    public String ID_autoComplete(int num) {
        if (num < 9) {
            return "T00000" + (num + 1);
        } else {
            if (num < 99) {
                return "T0000" + (num + 1);
            } else {
                if (num < 999) {
                    return "T000" + (num + 1);
                }
            }
        }
        return "Error";
    }
}
